package com.example.featuresdk.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject

@HiltViewModel
class SearchViewModel @Inject constructor(): ViewModel() {

    val uiState = SearchState()

    val commands = object: SearchCommands{
        override fun search(keyword: String) {

            if(keyword.contains("hi", ignoreCase = true)){
                uiState.results = flowOf(listOf("hi","hello","你好"))
            }else if(keyword.contains("good", ignoreCase = true)){
                uiState.results = flowOf(listOf("good","good morning","great"))
            }else{
                uiState.results = emptyFlow()
            }
        }

        override fun cancel() {
            SearchState.visible.value = false
            uiState.keyword.value = ""
        }
    }

    init {
        Log.e("SearchViewModel","init $this")
    }

    override fun onCleared() {
        Log.e("SearchViewModel","onCleared() $this")
    }
}